class RushController < ApplicationController
  require "facebook"
  layout "rush"

  before_filter :general_authorize
  before_filter :rush_authorize


  def index
    @this_houses_favorite_freshmen=@logged_in_user.house.favorite_freshmen
    max_checks = Constraint.getValueByKeyAndTime('ChecksPerHousePerHour',Time.now)
    current_checks = @logged_in_user.house.checks.count(["checked_at > ?", 1.hour.ago])
    @more_checks_allowed = max_checks > current_checks
    max_monitors = Constraint.getValueByKeyAndTime('MonitorsPerHouse',Time.now)
    current_monitors = @logged_in_user.house.favorite_freshmen.count(["last_monitor_check IS NOT NULL"]) 
    @more_monitors_allowed = max_monitors > current_monitors
  end
  
  def sort
    @this_houses_favorite_freshmen=@logged_in_user.house.favorite_freshmen
    @this_houses_favorite_freshmen.each do |freshman|
      freshman.position = params['favorite_freshmen'].index(freshman.id.to_s) + 1
      freshman.save
    end
    render :nothing =>true
  end
  
  def dynamic_freshman_search
    search_name = params[:search_name]
    if search_name == ''
      @possible_freshmen = [];
    else
      all_matches = search_name.split(' ').map do |name_fragment|
        name_fragment = '%' + name_fragment + '%'
        Freshman.find(:all, :conditions => ["first_name LIKE ? OR last_name LIKE ?", name_fragment, name_fragment])
      end
      @possible_freshmen = all_matches[0]
      all_matches.each do |these_matches|
        @possible_freshmen = @possible_freshmen & these_matches
      end
    end
    render(:layout=>false)
  end

  def run_check
    this_check = Check.create(:user_id => @logged_in_user.id, :freshman_id => params[:freshman_id], :checked_at => Time.now)
    this_monitor=@logged_in_user.house.favorite_freshmen.find_by_freshman_id(params[:freshman_id])
    unless this_monitor.nil? or this_monitor.last_monitor_check.nil?
      this_monitor.last_monitor_check=Time.now
      this_monitor.save!
    end
    flash[:freshman_id]=params[:freshman_id]
    redirect_to(:action => 'show_history')
  end

  def show_history
    @this_freshman_id = params[:freshman_id]
    @this_freshman_id ||= flash[:freshman_id]
    @all_checks=@logged_in_user.house.checks.find(:all,
    :conditions => ["freshman_id = ?", @this_freshman_id],
    :order => "checked_at DESC")
    render(:layout=>false)
  end

  def new_favorite
    @logged_in_user.house.favorite_freshmen.create(:freshman_id =>params[:freshman_id])
    redirect_to(:action => 'list_favorites')
  end

  def remove_favorite
    @logged_in_user.house.favorite_freshmen.find_by_freshman_id(params[:freshman_id]).destroy
    redirect_to(:action => 'list_favorites')
  end  

  def list_favorites
    @this_houses_favorite_freshmen=@logged_in_user.house.favorite_freshmen
    max_checks = Constraint.getValueByKeyAndTime('ChecksPerHousePerHour',Time.now)
    current_checks = @logged_in_user.house.checks.count(["checked_at > ?", 1.hour.ago])
    @more_checks_allowed = max_checks > current_checks
    max_monitors = Constraint.getValueByKeyAndTime('MonitorsPerHouse',Time.now)
    current_monitors = @logged_in_user.house.favorite_freshmen.count(["last_monitor_check IS NOT NULL"]) 
    @more_monitors_allowed = max_monitors > current_monitors
    render(:layout=>false)
  end 

  def set_monitor
    this_monitor=@logged_in_user.house.favorite_freshmen.find(params[:favorite_freshman_id])
    max_monitors=Constraint.getValueByKeyAndTime('MonitorsPerHouse',Time.now)
    current_monitors=@logged_in_user.house.favorite_freshmen.count(["(last_monitor_check IS NOT NULL) AND (id != ?)", params[:favorite_freshman_id]])
    unless this_monitor.nil?
      if current_monitors <= max_monitors
        this_monitor.last_monitor_check=Time.now
        this_monitor.save!
      end
    end
    redirect_to(:action => 'list_favorites')
  end


  def remove_monitor
    this_monitor=@logged_in_user.house.favorite_freshmen.find(params[:favorite_freshman_id])
    unless this_monitor.nil?
      this_monitor.last_monitor_check=nil
      this_monitor.save!
    end
    redirect_to(:action => 'list_favorites')
  end
  def callback
    f = Facebook::Session.new("e4e79b84c94a40cdac33f10a2315a8d2", "4e79b84c94a40cdac33f10a2315a8d2et")
    f.auth_token = params["auth_token"]
    results = f.photos.getOfUser({ :id => 700836 })
    image = results.first["src"]
  end
  private

  def rush_authorize
    unless @logged_in_user.nil? or @logged_in_user.rush_priv
      if request.xhr?
        redirect_to(:controller => 'auth', :action => 'xhr_auth_failure')
      else
        flash[:notice] = "Please select a section you are authorized for!"
        redirect_to(:controller => "auth")
      end
    end
  end



end
